package com.wang.android.library.ui;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;

import com.jaeger.library.StatusBarUtil;
import com.wang.android.library.frame.persenter.WBasePresenter;
import com.wang.android.library.tool.WEventCache;
import com.wang.android.library.tool.WTextUtils;
import com.wang.android.library.ui.dialog.WLoadingDialog;
import com.wang.android.library.ui.dialog.WNetLoadingDialog;

import butterknife.ButterKnife;

/**
 * @author Wanghuanyu
 * @date 2017/3/8 9:00
 * @description activity基类
 * @classname FBaseActivity
 * @version 1.0.0
 * @修改者
 * @时间
 */
public abstract class FBaseActivity<T extends WBasePresenter> extends FragmentActivity implements
        IFBaseView
{
    public View contextView;//布局
    private WLoadingDialog loadingDialog;//dialog
    private WNetLoadingDialog wNetLoadingDialog;
    public T presenter;//业务控制器
    public Intent intent;//上下文
    /**
     *
     * 开关
     */
    public boolean isInmmersiveMode = true;//4.4以上沉浸式开关默认开启   对应Activity根布局需要添加次属性android:fitsSystemWindows="true"
    public boolean RequestedOrientation = true;//是否禁止横屏

    /**
     * 布局文件
     * @return
     */
    protected abstract int getLayoutResource();
    /**
     * onCreate中操作
     */
    protected abstract void initTodo(Bundle savedInstanceState);
    /**
     * 监听设置（若使用ButterKnife请忽略）
     */
    protected abstract void setListeners();
    /**
     * setContent之前操作
     */
    public void beforeSetContentView() {

    }
    /**
     * setContent之后操作
     */
    public void afterSetContentView() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        register();
        presenter = WTextUtils.getT(this,0);
        if(presenter != null){
            presenter.setContext(this);
        }
        if(RequestedOrientation){
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//禁横屏
        }
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        if(getLayoutResource() != 0){
            contextView = LayoutInflater.from(this).inflate(getLayoutResource(),null);
            setContentView(contextView);
        }
        if (isInmmersiveMode && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        afterSetContentView();
        initStatusBar();

    /*    initSystemBarTint(contextView);//4.4兼容
        setLopStatBar(this, R.color.f_transparent_1a);//5.0+兼容设置statusBar 颜色*/
        ButterKnife.bind(this);
        initTodo(savedInstanceState);
        setListeners();
    }
    @Override
    public void showBaseDialog(){
        boolean dialogStatus = false;
        if (null != loadingDialog) {
            dialogStatus = loadingDialog.isShowing();
        }
        if (!dialogStatus) {
            loadingDialog = WLoadingDialog.show(this, false, new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                }
            });
        }
    }

    private void initStatusBar()
    {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
        {
            StatusBarUtil.setTranslucent(this);
        }
        else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
        {
            StatusBarUtil.setTranslucent(this);
        }
    }

    @Override
    public void dismissBaseDialog(){
        if (null != loadingDialog && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }

    @Override
    public void showNetDialog() {
        boolean dialogStatus = false;
        if (null != wNetLoadingDialog) {
            dialogStatus = wNetLoadingDialog.isShowing();
        }
        if (!dialogStatus) {
            wNetLoadingDialog = WNetLoadingDialog.show(this, false, new DialogInterface.OnCancelListener() {
                @Override
                public void onCancel(DialogInterface dialog) {

                }
            });
        }
    }

    @Override
    public void dismissNetDialog() {
        if (null != wNetLoadingDialog && wNetLoadingDialog.isShowing()) {
            wNetLoadingDialog.dismiss();
        }
    }

 /*   @TargetApi(19)
    private void initSystemBarTint(View view) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // create our manager instance after the content view is set
            mTintManager = new SystemBarTintManager(this);
            // enable status bar tint
            mTintManager.setStatusBarTintEnabled(true);
            // enable navigation bar tint
            mTintManager.setNavigationBarTintEnabled(true);
            mTintManager.setStatusBarAlpha(0.5f);
//            SystemBarTintManager.SystemBarConfig config = mTintManager.getConfig();
//            view.setPadding(0,config.getStatusBarHeight(),0,0);
//            this.getWindow().getDecorView().findViewById(android.R.id.content);
//            view.setPadding(0, config.getStatusBarHeight(), config.getPixelInsetRight(), config.getPixelInsetBottom());

        }
    }
    @TargetApi(21)
    public void setLopStatBar(Activity activity, int color) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN //Activity全屏显示，但状态栏不会被隐藏覆盖，状态栏依然可见，Activity顶端布局部分会被状态遮住。
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(ContextCompat.getColor(this,color));
//            window.setNavigationBarColor(Color.TRANSPARENT);
        }
    }*/

    @Override
    public void register()
    {
    }
    @Override
    public void unregister()
    {
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        unregister();
        super.onDestroy();
        ButterKnife.unbind(this);
    }

}
